package com.atguigu.springboot.service.impl;

import com.atguigu.springboot.entities.User;
import com.atguigu.springboot.entities.UserExample;
import com.atguigu.springboot.mapper.UserMapper;
import com.atguigu.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("userService")
public class UserServiceImpl  implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser(String uname, String upassword) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andUnameEqualTo(uname);
        List<User> users =userMapper.selectByExample(userExample);
        if(users==null||users.size()==0) return null;
        else return  users.get(0);
    }

    @Override
    public int addUser(User user) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andUnameEqualTo(user.getUname());
        if(userMapper.selectByExample(userExample).size()>0){
          return 0;
        }
        return userMapper.insert(user);
    }
}
